<?php
	class upLoad{
		private $fileName;//文件名
		private $fileType;//文件类型
		private $fileSize;//文件的大小
		private $fileTemp;//临时文件
		private $error;//上传是否有错误
		private $maxSize;//允许的最大上传文件尺寸
		private $directory;//文件最终存储目录
		private $newName;//规定存储在数据库的文件名
		private $arrType=array();//构造允许上传的文件类型
		public function __construct($fileField,$maxSize,$directory,$name=''){
			$this->fileName=$fileField['name'];
			$this->fileTemp=$fileField['tmp_name'];
			$this->error=$fileField['error'];
			$this->fileSize=$fileField['size'];
			$this->fileType=$fileField['type'];
			$this->maxSize=$maxSize;
			$this->directory=$directory;
			$this->newName=$name;
			if(empty($name)){
				$this->newName=time();
			}
			$this->setArray();
		}
		public function uploading(){
			if($this->error==0){
				if($this->fileSize <= $this->maxSize){
					if(in_array($this->fileType,$this->arrType)){
						if(file_exists($this->directory)){
							return $this->moveuploadedfile();
						}else {
								if(mkdir($this->directory,0775)){
								return	$this->moveuploadedfile();
								}else{
								return '无法找到'.$this->directory.'目录';
							}
						}
					}else{
						return '上传不支持'.$this->fileType.'类型';
					}
				}else{
					return '上传图片失败，请确认你的上传文件不超过 '.$this->maxSize;
				}
			}else if($this->error==1){
				 return '上传文件过大';
			}else if($this->error==2){
				return '上传文件过大';
			}else if($this->error==3){
				return '上传文件失败';
			}else if($this->error==4){
				return '上传文件失败';
			}else if($this->error==5){
				return '上传文件失败';
			}else if($this->error==6){
				return '上传文件失败';
			}else if($this->error==7){
				return '上传文件写入失败';
			}
		}
		private function moveuploadedfile(){
			$ext=substr($this->fileName, strrpos($this->fileName, '.'));
			$name=$this->newName.$ext;
			$dir=str_replace('//', '/', $this->directory.'/'.$name);
			$dir=str_replace('\\', '/', $this->directory.'/'.$name);
			if(move_uploaded_file($this->fileTemp,$dir)){
				return false;
			}else{
				return '上传图片失败，请确认你的上传文件不超过 '.$this->maxSize.'k或上传时间超时';
			}
		}
		private function setArray(){
			$this->arrType=array('image/jpg','image/gif','image/bmp','image/png','image/jpeg');
		}
	}
	/*$name='';
	if(!empty($_POST['img_name'])){
		$name=$_POST['img_name'];
	}
	$oUp=new upLoad($_FILES['upfile'], 1024*1204, './tmp/',$name);
	$msg=$oUp->uploading();
	if($msg==false){
		echo '上传成功';
	}else{
		echo "上传失败";
	}
	*/
?>
<!-- 
<form action="upfile.php" method="post" enctype="multipart/form-data">
				<table width="100%" border="1" cellspacing="0" cellpadding="0">
					<tr>
						<td class="left bold" width="100%">
							<h1>上传图片</h1>
						</td>
					</tr>
					<tr>
						<td class="left">
							<label for="img_name">图片名称:</label><input type="text" name="img_name" id="img_name"/>
							<label for="upfile" class="upload">最大上传的文件尺寸1M</label>
							<input type="file" name="upfile" id="upfile" value="选择文件"/>
							<div class="exthr">
								<input type="submit" name="sub_img" value="上传图片"/>
								<input type="reset" name="reset_img" value="取消"/>
							</div>
						</td>
					</tr>
				</table>
</form>
-->